package org.jeecg.modules.htxincailiao.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.RequestUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.htxincailiao.account.service.IHTAccountService;
import org.jeecg.modules.htxincailiao.goods.entity.HTGoods;
import org.jeecg.modules.htxincailiao.goods.entity.HTGoodsCategory;
import org.jeecg.modules.htxincailiao.goods.service.IHTGoodsCategoryService;
import org.jeecg.modules.htxincailiao.goods.service.IHTGoodsService;
import org.jeecg.modules.htxincailiao.shop.service.IHTShopService;
import org.jeecg.modules.permissions.HTRolePermission;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: ht_goods
 * @Author: jeecg-boot
 * @Date: 2021-11-04
 * @Version: V1.0
 */
@Api(tags = "ht_goods")
@RestController
@RequestMapping("/goods/hTGoods")
@Slf4j
public class HTGoodsController extends JeecgController<HTGoods, IHTGoodsService> {
    @Resource
    private IHTGoodsService hTGoodsService;
    @Resource
    private IHTGoodsCategoryService goodsCategoryService;
    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysRoleService sysRoleService;

    @Resource
    private ISysUserRoleService sysUserRoleService;

    @Resource
    private IHTAccountService accountService;

    @Resource
    private IHTShopService shopService;

    /**
     * 分页列表查询
     *
     * @param hTGoods
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_goods-分页列表查询")
    @ApiOperation(value = "ht_goods-分页列表查询", notes = "ht_goods-分页列表查询")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPageList(HTGoods hTGoods,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTGoods = JSONObject.parseObject(line, HTGoods.class);
                pageNo = oConvertUtils.isNotEmpty(hTGoods.getPageNo()) ? hTGoods.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTGoods.getPageSize()) ? hTGoods.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(hTGoods) &&
                oConvertUtils.isNotEmpty(hTGoods.getCategoryId()) &&
                hTGoods.getCategoryId().equals("0")) {
            //全部分类
            hTGoods.setCategoryId(null);
        }

        HTRolePermission.AddGoodsPermission(req, hTGoods);

        QueryWrapper<HTGoods> queryWrapper = QueryGenerator.initQueryWrapper(hTGoods, req.getParameterMap());
        queryWrapper.orderByDesc("update_time");

        Page<HTGoods> page = new Page<HTGoods>(pageNo, pageSize);
        IPage<HTGoods> pageList = hTGoodsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 分页列表查询
     *
     * @param hTGoods
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ht_goods-分页列表查询")
    @ApiOperation(value = "ht_goods-分页列表查询", notes = "ht_goods-分页列表查询")
    @RequestMapping(value = "/pc-home-list", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryPCHomePageList(HTGoods hTGoods,
                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                         HttpServletRequest req) {
        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                hTGoods = JSONObject.parseObject(line, HTGoods.class);
                pageNo = oConvertUtils.isNotEmpty(hTGoods.getPageNo()) ? hTGoods.getPageNo() : pageNo;
                pageSize = oConvertUtils.isNotEmpty(hTGoods.getPageSize()) ? hTGoods.getPageSize() : pageSize;
            }
        }

        if (oConvertUtils.isNotEmpty(hTGoods) &&
                oConvertUtils.isNotEmpty(hTGoods.getCategoryId()) &&
                hTGoods.getCategoryId().equals("0")) {
            //全部分类
            hTGoods.setCategoryId(null);
        }

        HTRolePermission.AddGoodsPermission(req, hTGoods);

        QueryWrapper<HTGoods> queryWrapper = QueryGenerator.initQueryWrapper(hTGoods, req.getParameterMap());
//        queryWrapper.orderByDesc("update_time");
        queryWrapper.orderByDesc("is_home_recommended").orderByDesc("update_time");

        Page<HTGoods> page = new Page<HTGoods>(pageNo, pageSize);
        IPage<HTGoods> pageList = hTGoodsService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param hTGoods
     * @return
     */
    @AutoLog(value = "ht_goods-添加")
    @ApiOperation(value = "ht_goods-添加", notes = "ht_goods-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody HTGoods hTGoods) {
        hTGoodsService.save(hTGoods);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param hTGoods
     * @return
     */
    @AutoLog(value = "ht_goods-编辑")
    @ApiOperation(value = "ht_goods-编辑", notes = "ht_goods-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody HTGoods hTGoods) {
        hTGoodsService.updateById(hTGoods);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_goods-通过id删除")
    @ApiOperation(value = "ht_goods-通过id删除", notes = "ht_goods-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        hTGoodsService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ht_goods-批量删除")
    @ApiOperation(value = "ht_goods-批量删除", notes = "ht_goods-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.hTGoodsService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ht_goods-通过id查询")
    @ApiOperation(value = "ht_goods-通过id查询", notes = "ht_goods-通过id查询")
    @RequestMapping(value = "/queryById", method = {RequestMethod.GET, RequestMethod.POST})
    public Result<?> queryById(HttpServletRequest req, @RequestParam(name = "id", required = false) String id) {

        if (req.getMethod().equals("POST")) {
            String line = RequestUtil.getPostParam(req);
            if (oConvertUtils.isNotEmpty(line)) {
                HTGoods hTGoods = JSONObject.parseObject(line, HTGoods.class);
                id = hTGoods.getId();
            }
        }

        HTGoods hTGoods = hTGoodsService.getById(id);

        if (hTGoods == null) {
            return Result.error("未找到对应数据");
        }
        //查询商品分类
        HTGoodsCategory goodsCategory = goodsCategoryService.getById(hTGoods.getCategoryId());
        if (goodsCategory != null) {
            hTGoods.setCategoryName(goodsCategory.getName());
        }
        return Result.OK(hTGoods);
    }


    /**
     * 导出excel
     *
     * @param request
     * @param hTGoods
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, HTGoods hTGoods) {
        return super.exportXls(request, hTGoods, HTGoods.class, "ht_goods");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, HTGoods.class);
    }

}
